using CampusDataOperationLibrary;
using CampusSignInManageSystemWebAPI.Command.LoginCommand;
using CampusSignInManageSystemWebAPI.Responses.Student;
using Microsoft.AspNetCore.Mvc;
using PublicProject.PublicClass.OutPutModels;
using PublicProject.PublicMethods;

namespace CampusSignInManageSystemWebAPI.Controllers;

[ApiController]
[Route("[controller]/[action]")]
public class LoginController(LoginOperation loginOperation)
{
    private readonly LoginOperation _loginOperation = loginOperation;
    [HttpPost]
    public async Task<OutputModel<string>> RegisterUser([FromBody] Register input)
    {
        if (string.IsNullOrEmpty(input.Username) || string.IsNullOrEmpty(input.Password))
        {
            return OutPutMethod<string>.Failure("用户名或密码不能为空");
        }
        return OutPutMethod<string>.Success(await this._loginOperation.RegisterUser(input.Username, input.Password, input.Role, input.StudentClassName), message: "注册成功");
    }

    [HttpPost]
    public async Task<OutputModel<string>> Login([FromBody] Login input)
    {
        if (string.IsNullOrEmpty(input.Username) || string.IsNullOrEmpty(input.Password))
        {
            return OutPutMethod<string>.Failure("用户名或密码不能为空");
        }
        var result = await this._loginOperation.LoginUser(input.Username, input.Password);
        return result == null ? OutPutMethod<string>.Failure("用户名或密码错误") : OutPutMethod<string>.Success(result, message: "登录成功");
    }

    [HttpGet]
    public async Task<OutputModel<List<RegisterClass>>> GetRegisterClasses()
    {
        var result =  await this._loginOperation.GetClassesAsync();
        return OutPutMethod<List<RegisterClass>>.Success(result.Select(x => new RegisterClass { ClassName = x.ClassName }).ToList(), message: "获取成功");
    }
}